<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Model.php');
require_once($Config->WebCMSRoot.'Data/Image.php');

define('TNMAX', 150);
define('JPEGQUALITY', 95);
define('RESIZEFACTOR', 0.1);

Class ImageModel extends BaseModel
{
	var $File;
	var $Thumbnail;
	var $Width;
	var $Height;
	var $ThumbnailWidth;
	var $ThumbnailHeight;
	var $Module;
	
	var $_parentID;
	
	function ImageModel()
	{
		$this->File			   = "";
		$this->Thumbnail	   = "";
		$this->Width 		   = "";
		$this->Height 		   = "";
		$this->ThumbnailWidth  = "";
		$this->ThumbnailHeight = "";
		$this->Module          = "";
		
		$this->_parentID        = -1;
		
		parent::BaseModel();
	}
	
	function Retrieve(&$parameters)
	{
		$db = new ImageData();
		$this->Parameters = &$parameters;

		$this->ID   		   = $this->GetParameter('id');
		$this->File 		   = $this->GetParameter('file');
		$this->Thumbnail	   = $this->GetParameter('thumbnail');
		$this->Width		   = $this->GetParameter('width');
		$this->Height		   = $this->GetParameter('height');
		$this->ThumbnailWidth  = $this->GetParameter('thumbWidth');
		$this->ThumbnailHeight = $this->GetParameter('thumbHeight');
		$this->Module          = $this->GetParameter('module');
		
		$db->Retrieve(&$this);
//print('<pre>'.var_export($this, true).'</pre>');exit;
	}
	
	function Upload($image, $ImageFolder, $thumbnailFolder=null, $tnmax=TNMAX)
	{
		global $Config;

		if ($image['size'] != 0)
		{
			$this->File = $ImageFolder.$image['name'];
			move_uploaded_file($image['tmp_name'], substr($Config->DocumentRoot, 0, -1) . $this->File);
			
			// image size
			$size = getimagesize(substr($Config->DocumentRoot, 0, -1) . $this->File);
			$this->Width = $size[0];
			$this->Height = $size[1];
			
			if ($thumbnailFolder != null)
			{
				$this->Thumbnail = $thumbnailFolder.$image['name'];
				$thumb = $this->CreateThumbnail(substr($Config->DocumentRoot, 0, -1) . $this->File, substr($Config->DocumentRoot, 0, -1) . $this->Thumbnail, $tnmax, RESIZEFACTOR);

				// thumbnail size
				$size = getimagesize(substr($Config->DocumentRoot, 0, -1) . $this->Thumbnail);
				$this->ThumbnailWidth = $size[0];
				$this->ThumbnailHeight = $size[1];
			}
			
			// store in db
			$db = new ImageData();
			$db->Update(&$this);
        }
	}
	
	function CreateThumbnail($filenameIn, $filenameOut, $max, $_X_resizeFactor, $enlarge = false)
	{
//print('1:'.$filenameIn."\n");
//print('2:'.$filenameOut."\n");
//print('3:'.$max."\n");
//print('4:'.$enlarge."\n");
//exit;
		$size = getimagesize($filenameIn);
		$newSize = array($size[0], $size[1]);
		$resizeFactor = 1;

		if ($size[0] >= $size[1]) {
			$resizeFactor = $max / $size[0];
		} else {
			$resizeFactor = $max / $size[1];
		}
		if ($resizeFactor > 1 && !$enlarge) {
			$resizeFactor = 1;
		}
		
		$newSize[0] = round($size[0] * $resizeFactor);
		$newSize[1] = round($size[1] * $resizeFactor);

		$fileinfo = pathinfo($filenameIn);
		switch (strtolower($fileinfo['extension'])) {
			case 'gif':
				$im_in = imagecreatefromgif($filenameIn);
				break;
				
			default:
				$im_in = imagecreatefromjpeg($filenameIn);
				break;
		}

		$im_out = imagecreatetruecolor($newSize[0], $newSize[1]);
		imagecopyresampled($im_out, $im_in, 0, 0, 0, 0, $newSize[0], $newSize[1], $size[0], $size[1]);

		switch (strtolower($fileinfo['extension'])) {
			case 'gif':
				imagegif($im_out, $filenameOut);
				break;
				
			default:
				imagejpeg($im_out, $filenameOut, JPEGQUALITY);
				break;
		}
		
		imagedestroy($im_in);
		imagedestroy($im_out);

		return($im_out);
	}
	/* Not yet implemented
	function Delete()
	{
		$db = new ImageData();
		$db->Delete(&$this);
	}
	*/
}
?>